<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>first vue</title>
</head>
<body>

<div id="example-1">
    <button v-on:click="counter += 1">Add 1</button>
    <p>The button above has been clicked {{ counter }} times.</p>
</div>

<div id="example-2">
    <!-- `greet` 是在下面定义的方法名 -->
    <button v-on:click="greet">Greet</button>
</div>

<div id="example-3">
    <button v-on:click="say('hi')">Say hi</button>
    <button v-on:click="say('what')">Say what</button>
</div>

<div id="example-4">
    <button v-on:click="warn('Form cannot be submitted yet.', $event)">
        Submit
    </button>
</div>

<div id="dynamic-component-demo" class="demo">
    <button
            v-for="tab in tabs"
            v-bind:key="tab"
            v-bind:class="['tab-button', { active: currentTab === tab }]"
            v-on:click="currentTab = tab">
        {{tab}}
    </button>

    <component v-bind:is="currentTabComponent" class="tab"></component>
</div>

<div id="slot-component-demo" class="demo">
    <todo-s>
        <todo-title slot="todo-title" v-bind:title="title"></todo-title>
        <todo-text slot="todo-text" v-for="(item,index) in items" v-bind:item="item"
                   v-bind:index="index" v-bind:key="index" v-on:remove="removeItem(index)"></todo-text>
    </todo-s>
</div>

<!--导入vue.js-->
<script src="../../resources/js/vue.min.js"></script>
<!--<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>-->
<script>
    let example1 = new Vue({
        el: '#example-1',
        data: {
            counter: 0
        }
    });

    var example2 = new Vue({
        el: '#example-2',
        data: {
            name: 'Vue.js'
        },
        // 在 `methods` 对象中定义方法
        methods: {
            greet: function (event) {
                // `this` 在方法里指向当前 Vue 实例
                alert('Hello ' + this.name + '!')
                // `event` 是原生 DOM 事件
                if (event) {
                    alert(event.target.tagName)
                }
            }
        }
    });

    let vm3 = new Vue({
        el: '#example-3',
        methods: {
            say: function (message) {
                alert(message)
            }
        }
    });

    let vm4 = new Vue({
        el: '#example-4',
        methods: {
            warn: function (message, event) {
                // 现在我们可以访问原生事件对象
                if (event) {
                    event.preventDefault();
                    alert(event.target.name)
                }
                alert(message)
            }
        }
    });

    //动态组件
    Vue.component("tab-home", {
        template: "<div>Home component</div>"
    });
    Vue.component("tab-posts", {
        template: "<div>Posts component</div>"
    });
    Vue.component("tab-archive", {
        template: "<div>Archive component</div>"
    });

    new Vue({
        el: "#dynamic-component-demo",
        data: {
            currentTab: "Home",
            tabs: ["Home", "Posts", "Archive"]
        },
        computed: {
            currentTabComponent: function() {
                return "tab-" + this.currentTab.toLowerCase();
            }
        }
    });

    //插槽
    Vue.component("todo-s", {
        template: "<div><slot name='todo-title'></slot><ul><slot name='todo-text'></slot></ul></div>"
    });
    Vue.component("todo-title", {
        props:['title'],
        template: "<div>{{title}}</div>"
    });
    Vue.component("todo-text", {
        props: ['item','index'],
        template: "<li>{{index}}---{{item}}<button v-on:click='remove'>删除</button></li>",
        methods:{
            remove: function (index) {
                this.$emit("remove",index);
            }
        }
    });

    new Vue({
        el: "#slot-component-demo",
        data: {
            title: "vue",
            items: ["Home", "Posts", "Archive"]
        },
        methods: {
            removeItem: function (index) {
                this.items.splice(index,1);
            }
        }
    });

</script>
</body>
</html>